package com.tsn.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsn.common.Result;
import com.tsn.entity.Comment;
import com.tsn.service.CommentService;
import com.tsn.service.ForumService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author tsn
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/tsn/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ForumService forumService;

    @GetMapping("getComments")
    public Result getComments(@RequestParam("type")Integer type,@RequestParam("id")Integer id){
        return commentService.getComment(type,id);
    }
    @GetMapping("getCommentList")
    public Result getCommentList(){
        return Result.success(commentService.list());
    }
    @RequiresAuthentication
    @PostMapping("addComment")
    public Result addComment(@RequestBody JSONObject object){
        Comment comment = JSON.toJavaObject(object, Comment.class);
        commentService.save(comment);
        if(comment.getType()==0)
        {
            forumService.addCommentNumber(comment.getArticleId());
        }
//        JSON.toJavaObject(group,Group.class);
        return Result.success("添加成功！",null);
    }
    @GetMapping("/getMyComments")
    public Result getMyComments(@RequestParam("userId")Integer userId){
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("user_id",userId);
        List<Comment> list = commentService.list(commentQueryWrapper);
        return Result.success(list);
    }
    @GetMapping("getForumComments")
    public Result getForumComments(@RequestParam("id")Integer id){
        return commentService.getForumComment(id);
    }
    @RequiresAuthentication
    @PostMapping("/deleteComments")
    public Result deleteComments(@RequestBody JSONObject object){
        Integer id = object.getInteger("id");
        commentService.removeById(id);
        return Result.success(null);
    }
}

